package cn.itcast.controller.system;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.system.Module;
import cn.itcast.service.system.ModuleService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/system/module")
public class ModuleController extends BaseController {

    @Autowired
    private ModuleService moduleService;

    @RequestMapping(value = "/list",name ="查询所有模块菜单数据")
    public String findAll(@RequestParam(defaultValue = "1",name = "page") int pageNum,@RequestParam(defaultValue = "10") int pageSize){
//        List<Module> moduleList = moduleService.findAll();
//        PageBean pageBean = moduleService.findPage(pageNum,pageSize);
        PageInfo<Module> pageBean = moduleService.findPage(pageNum,pageSize);

        request.setAttribute("page",pageBean);
        return "system/module/module-list";
    }
    @RequestMapping(value = "/toAdd",name ="进入到添加模块菜单的页面")
    public String toAdd(){
//        查询所有的菜单
        List<Module> moduleList = moduleService.findAll();
        request.setAttribute("menus",moduleList);
        return "system/module/module-add";
    }

    @RequestMapping(value = "/edit",name ="保存模块菜单数据") //新增或修改时都会进入这个方法
    public String edit(Module module){

        if(StringUtils.isEmpty(module.getId())){
            //id为空新增 save
            module.setId(UUID.randomUUID().toString());
            moduleService.save(module);
        }else{
            moduleService.update(module); //id如果有值 update
        }
//        不管新增还是修改都是要重定向到列表页面
        return "redirect:/system/module/list.do";  //redirect 重定向  在浏览器发起的请求
    }

    @RequestMapping(value = "/toUpdate",name ="进入到编辑模板菜单页面")
    public String toUpdate(String id){
        Module module = moduleService.findById(id);
        request.setAttribute("module",module);

        List<Module> moduleList = moduleService.findAll();
        request.setAttribute("menus",moduleList);


        return "system/module/module-add";
    }
    @RequestMapping(value = "/delete",name ="根据id删除模块菜单数据")
    public String delete(String id){
        moduleService.delete(id);

        return "redirect:/system/module/list.do";
    }




}
